package com.putong35.easyui.ric;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;


/**
 * Simple to Introduction
 *
 * @Author: PengWeiwei
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2017-04-11
 * @Version: 1.0.0
 */
public class CameraPermissionFragment extends Fragment implements ActivityCompat.OnRequestPermissionsResultCallback{

    public static final String TAG = CameraPermissionFragment.class.getSimpleName();

    private static final int PERMISSIONS_REQUEST_TAKE_PHOTO = 1;
    private static final int PERMISSIONS_REQUEST_CHOSE_PHOTO = 2;

    public CameraPermissionFragment() {
        // Required empty public constructor
    }

    public static CameraPermissionFragment newInstance() {
        CameraPermissionFragment fragment = new CameraPermissionFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }

        requestCameraPermissions();

    }

    private void requestCameraPermissions() {
        List<String> needPermissions = new ArrayList<String>();
        if (ContextCompat.checkSelfPermission(getContext(),
                Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            needPermissions.add(Manifest.permission.CAMERA);
        }

        if (ContextCompat.checkSelfPermission(getContext(),
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            needPermissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }

        if (needPermissions.size() > 0) {
            String[] needPerArray = needPermissions.toArray(new String[needPermissions.size()]);
//            ActivityCompat.requestPermissions(getActivity(), needPerArray, PERMISSIONS_REQUEST_TAKE_PHOTO);


            if (getActivity() != null) {
                ActivityCompat.requestPermissions(getActivity(), needPerArray, PERMISSIONS_REQUEST_TAKE_PHOTO);
                requestPermissions(needPerArray, PERMISSIONS_REQUEST_TAKE_PHOTO);
            }
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == PERMISSIONS_REQUEST_TAKE_PHOTO) {
            if (acceptAllPermissions(permissions, grantResults)) {
                Log.e(TAG, "onRequestPermissionsResult: 成功获取照相机权限");
            } else {
                // Permission Denied
                Toast.makeText(getContext(), "获取照相机权限失败", Toast.LENGTH_SHORT).show();
            }
        }


        if (requestCode == PERMISSIONS_REQUEST_CHOSE_PHOTO) {
            if (acceptAllPermissions(permissions, grantResults)) {
                Log.e(TAG, "onRequestPermissionsResult: 成功获取图库权限");
                // todo

            } else {
                // Permission Denied
                Toast.makeText(getContext(), "用户拒绝打开图库权限", Toast.LENGTH_SHORT).show();
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }



    boolean acceptAllPermissions(String[] permissions, int[] grantResults) {
        if (grantResults != null && grantResults.length > 0) {
            for (int grantResult : grantResults) {
                if (grantResult != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }

        return true;
    }


}
